﻿/***********************************************/
//页面状态
var State;
//控件信息集合
var ControlsCollection;
//当前明细的Keyid
var Keyid;

$(document).ready(function () {
    top.hiedeProgressBar(); //选项卡页加载完毕，关闭进度条
    declareControlsCollection();
    processState($.query.get('State'));
});

//声明控件信息结合
var declareControlsCollection = function () {
    var collection = [
        New(ASPxRFLabel, ['lblKeyid', ['GroupA'], 'Keyid']),
        New(ASPxRFLabel, ['lblParentKeyid', ['GroupA'], 'ParentKeyid']),
        New(ASPxRFTextBox, ['txtAreaCode', ['GroupA'], 'AreaCode']),
        New(ASPxRFTextBox, ['txtAreaName', ['GroupA'], 'AreaName']),
        New(ASPxRFTextBox, ['txtHelpCode', ['GroupA'], 'HelpCode']),
        New(ASPxRFTextBox, ['txtAdminAreacd', ['GroupA'], 'AdminAreacd']),
        New(ASPxRFTextBox, ['txtAdminAreaName', ['GroupA'], 'AdminAreaName']),
        New(ASPxRFRadioButtonList, ['rblIsSys', ['GroupA'], { valueField: 'IsSys' }, { valueFieldType: 'Boolean'}])
    ];
    ControlsCollection = New(RapidForm, [collection]);
}

//处理页面状态
var processState = function () {
    //设置页面状态
    State = arguments[0];
    if (State == 'ADD') {
        afterAdd();
        //菜单禁用
        doSetMenuItemEnable(false);
        //选中菜单中新增按钮
        doSelectMenuItem('btnAdd');
        //显示新增对应操作按钮（保存，保存完继续新增，重置）
        $('.ui-btn-saveforadd').addClass('ui-btn-bottom-selected');
        //获取新的Keyid
        lblKeyid.SetText($.newGuid());
        var areaKeyid;
        if (arguments.length == 1) {
            areaKeyid = $.query.get("parentKeyid");
            lblParentKeyid.SetText(areaKeyid);
        }
        //新增后默认设置值
        afterAdd();
    } else if (State == 'VIEW') {//添加-》查看； 编辑-》查看； 概览-》查看
        //启用Menu按钮
        doSetEnableAndSelectMenuItem(['btnAdd', 'btnEdit', 'btnDelete', 'btnPrint'], true);
        //让底部操作按钮不可见
        $('.ui-btn-bottom-selected').removeClass('ui-btn-bottom-selected');
        //让表单元素不可编辑
        ControlsCollection.setEnabled(false, 'GroupA');
    } else if (State == 'EDIT') {
        afterEdit();
        //编辑按钮可用并且被选中，其他按钮禁用
        doSetEnableAndSelectMenuItem(['btnAdd', 'btnDelete', 'btnPrint'], false, 'btnEdit');
        //显示新增对应操作按钮（保存，取消）
        $('.ui-btn-saveforedit').addClass('ui-btn-bottom-selected');
        var keyid;
        if (arguments.length == 1) {
            keyid = $.query.get('Keyid');
        } else if (arguments.length == 2) {
            keyid = arguments[1];
        } else {
            throw new Error('processState方法的参数错误');
        }
        //显示明细记录
        SGRapidForm.Foundation.AreaMaintanceDetail.GetAreaByKeyid(keyid, function (result) {
            var data = result.value;
            ControlsCollection.update(data, 'GroupA');
        });
    } else if (State == 'LOAD') {
        //启用Menu按钮
        doSetEnableAndSelectMenuItem(['btnAdd', 'btnEdit', 'btnDelete', 'btnPrint'], true);
        //让底部操作按钮不可见
        $('.ui-btn-bottom-selected').removeClass('ui-btn-bottom-selected');

        if (arguments.length == 1) {
            keyid = $.query.get('Keyid');
        } else if (arguments.length == 2) {
            keyid = arguments[1];
        } else {
            throw new Error('processState方法的参数错误');
        }

        //显示明细记录
        SGRapidForm.Foundation.AreaMaintanceDetail.GetAreaByKeyid(keyid, function (result) {
            var data = result.value;
            ControlsCollection.update(data, 'GroupA');
        });

        //让表单元素不可编辑
        ControlsCollection.setEnabled(false, 'GroupA');
    }
}

//启用或禁用指定按钮 
function doSetMenuItemEnable(val) {
    var menuNameCollection = ['btnEdit', 'btnDelete', 'btnDelete', 'btnPrint'];
    for (var item in menuNameCollection) {
        var menuItem = menuHeader.GetItemByName(menuNameCollection[item]);
        if (menuItem) {
            menuItem.SetEnabled(val);
        }
    }
}

//启用或禁用指定按钮 
//arguments[0] 按钮名称字符串数组
//arguments[1] 是否启用
//arguments[2] 被选中按钮的名称
function doSetEnableAndSelectMenuItem() {
    //启用或者禁用指定的Menu按钮
    for (var i = 0; i < arguments[0].length; i++) {
        var item = menuHeader.GetItemByName(arguments[0][i]);
        if (item) {
            item.SetEnabled(arguments[1]);
        }
    }
    if (arguments.length == 3) {
        //选中指定的Menu按钮
        var selectedItem = menuHeader.GetItemByName(arguments[2]);
        if (selectedItem) {
            selectedItem.SetEnabled(true);
            menuHeader.SetSelectedItem(selectedItem);
        }
    }
}

//选中Menu中指定的按钮
var doSelectMenuItem = function (itemName) {
    menuHeader.SetSelectedItem(menuHeader.GetItemByName(itemName));
}

/*********** 新增操作 ************/
//新增后默认设置
var afterAdd = function () {
    rblIsSys.SetSelectedIndex(1);
    txtAreaCode.SetText('自动生成');
    txtAreaCode.SetEnabled(false);
    txtAreaName.Focus();
}

//新增
var doAdd = function () {
    if (State == 'ADD') {
        parent.alertMsg.warn('您已经处于新增状态！');
        return;
    }
    //让新增的表单元素可编辑
    ControlsCollection.setEnabled(true, 'GroupA');
    //重置新增的表单元素,不需要重置lblKeyid控件
    ControlsCollection.reset('GroupA', 'lblKeyid');
    //处理新增状态界面
    processState('ADD');
}

//新增的保存
var doSaveForAdd = function () {
    //先验证
    if (ASPxClientEdit.ValidateEditorsInContainerById('form1')) {
        var area = ControlsCollection.get('GroupA');
        SGRapidForm.Foundation.AreaMaintanceDetail.SaveForAdd(area, function (result) {
            if (result.value) {
                $.messager.show({
                    title: '信息提示',
                    msg: '新增成功',
                    showType: 'slide'
                });
                //处理页面状态
                processState('VIEW', lblKeyid.GetText());
            } else {
                $.messager.show({
                    title: '信息提示',
                    msg: '新增失败!',
                    showType: 'slide'
                });

            }
        });
    }
}
//保存完继续新增 保存，重置全部控件，获取Keyid
var doSaveAndContinueToAdd = function () {
    //先验证
    if (ASPxClientEdit.ValidateEditorsInContainerById('form1')) {
        var area = ControlsCollection.get('GroupA');
        SGRapidForm.Foundation.AreaMaintanceDetail.SaveForAdd(area, function (result) {
            if (result.value) {
                $.messager.show({
                    title: '信息提示',
                    msg: '新增成功！请继续新增..',
                    showType: 'slide'
                });
                //重置全部控件
                ControlsCollection.reset('GroupA');
                //获取新的Keyid
                lblKeyid.SetText($.newGuid());
            } else {
                $.messager.show({
                    title: '信息提示',
                    msg: '新增失败！',
                    showType: 'slide'
                });
            }
        });
    }
}
//新增的重置
var doResetForAdd = function () {
    //不需要重置lblKeyid控件
    ControlsCollection.reset('GroupA', 'lblKeyid');
}


/*********** 编辑操作 ************/
//编辑后
var afterEdit = function () {
    txtAreaCode.SetEnabled(false);
    txtAreaName.Focus();
}
//编辑
var doEdit = function () {
    afterEdit();
    if (State == 'EDIT') {
        
        parent.alertMsg.warn('您已经处于编辑状态！');
        return;
    }
    processState('EDIT', lblKeyid.GetText());
    //让编辑的表单元素可编辑
    ControlsCollection.setEnabled(true, 'GroupA');
}
//编辑的保存
var doSaveForEdit = function () {
    //先验证
    if (ASPxClientEdit.ValidateEditorsInContainerById('form1')) {
        var user = ControlsCollection.get('GroupA');
        SGRapidForm.Foundation.AreaMaintanceDetail.SaveForEdit(user, function (result) {
            if (result.value) {
                $.messager.show({
                    title: '信息提示',
                    msg: '编辑成功'
                });
                //处理页面状态
                processState('VIEW', lblKeyid.GetText());
            } else {
                $.messager.show({
                    title: '信息提示',
                    msg: '编辑失败'
                });
            }
        });
    }
}
//取消编辑
var doCancelForEdit = function () {
    processState('LOAD', lblKeyid.GetText());
    //取消验证
    ASPxClientEdit.ClearEditorsInContainerById('form1');
}

/*********** 删除操作 ************/
//删除
var doDelete = function () {
    parent.alertMsg.confirm("确认删除吗?", {
        //确定删除
        okCall: function () {
            SGRapidForm.Foundation.AreaMaintanceDetail.Delete(lblKeyid.GetText(), function (result) {
                if (result.value) {
                    $.messager.show({
                        title: '信息提示',
                        msg: '删除成功',
                        showType: 'slide'
                    });
                    parent.navTab.closeCurrentTab();

                } else {
                    $.messager.show({
                        title: '信息提示',
                        msg: '删除失败',
                        showType: 'slide'
                    });
                }
            });
        }
    });
}
